package com.example.qrcodecamera;

import android.content.Context;
import android.net.Uri;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class CSVHelper {
    private static final String TAG = "CSVHelper";

    // 从URI导入CSV文件
    public static List<UserAccount> importCSV(Context context, Uri uri) {
        List<UserAccount> accounts = new ArrayList<>();
        try {
            InputStream inputStream = context.getContentResolver().openInputStream(uri);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            
            // 跳过标题行
            reader.readLine();
            
            while ((line = reader.readLine()) != null) {
                String[] data = line.split(",");
                if (data.length >= 2) {
                    String username = data[0].trim();
                    String password = data[1].trim();
                    accounts.add(new UserAccount(username, password));
                }
            }
            reader.close();
            inputStream.close();
        } catch (IOException e) {
            Log.e(TAG, "Error reading CSV file", e);
        }
        return accounts;
    }
} 